package backstage.app.controller;
 
import java.text.ParseException;


import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 

import backstage.app.entity.AllStudent;
import backstage.app.entity.NoStudent;
import backstage.app.entity.StudentEcharts;
import backstage.app.mapper.NoStudentMapper;
import backstage.app.mapper.NoStudentTypeMapper;
 
import backstage.app.respont.RespontDateJson;
import backstage.app.respont.RespontDateJsonStu;
import backstage.app.service.impl.util.NoStudentImplUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("${spring.application.name}/no-student/api")
@Api("非学员-控制器")
public class NoStudentController {
	
	 
	@Autowired
	private NoStudentMapper noStudentMapper;
	@Autowired
	private NoStudentTypeMapper nostudentTypeMapper;
	@Autowired
	private NoStudentImplUtil noStudentImplUtil;
	
	private static final Logger logger = (Logger) LoggerFactory.getLogger(NoStudentController.class);
	
	
	@GetMapping("/count")
	@ApiOperation("统计数量获取")
	public RespontDateJson<?>count(){
		AllStudent allStudent = null;
		try {
			  allStudent=noStudentMapper.selectAllStudent();
			  return RespontDateJson.ok(allStudent, "成功");
		} catch (Exception e) {
			logger.error("error in {}",allStudent);
			return RespontDateJson.ok(allStudent, "异常出现");
		}
		 
		 
	}
	
	
	@GetMapping("/get/echarts")
	@ApiOperation("统计数量获取")
	public RespontDateJsonStu echarts(){
		List<StudentEcharts> list = null;
		try {
			list=noStudentMapper.selectEchartsList();
			int[]count=new int[list.size()];
			String[]date=new String[list.size()];
			for (int i = 0; i < list.size(); i++) {
				count[i]=list.get(i).getTotal();
				date[i]=list.get(i).getDate();
			}
			return RespontDateJsonStu.ok(count,date, "成功");
		} catch (Exception e) {
			logger.error("error in {}",list);
			return RespontDateJsonStu.ok(null,null, "统计数据时异常出现");
		}
		 
		 
	}
	
	@GetMapping("/page")
	@ApiOperation(value="学员列表分页", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name",value = "名字",required = false),
		@ApiImplicitParam(name = "studentType",value = "标签",required = false),
		@ApiImplicitParam(name = "page", value = "起始页默认是0", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "limit", value = "总大小页数默认是20", required = false, dataType = "Integer"),	 
	})
	public RespontDateJson<?> page(
			@RequestParam(value="name",required = false)String name,
			@RequestParam(name = "studentType",required = false)Integer studentType,
			@RequestParam(value = "page",required = false,defaultValue = "0") Integer page,
			@RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit	 			
			) {
		 	 
		long total=noStudentMapper.selectCount(null);
		List<NoStudent>list;
		if(studentType!=null&&studentType==0) {
			list=noStudentMapper.selectlimitAndWhereNostudentType(name, page==1?page=0:page, limit);
			for (NoStudent student : list) {
				student.setTypeName(student.getTypeStudent().getNames());
			}
		}else {
			/**存在便签时**/
			 list=noStudentMapper.selectlimitAndWhere(name, studentType, page==1?page=0:page, limit);
			 for (NoStudent student : list) {
					student.setTypeName( nostudentTypeMapper.selectByStudentType(student.getStudentType()).getNames());
			}
		}	 
		 
		return RespontDateJson.ok(list, total,"查询命中成功");
	}
	
	/***
	 * 
	 * @param name
	 * @param sex
	 * @param phone
	 * @param date
	 * @param studentType
	 * @return
	 */
	@PostMapping("/inser")
	@ApiOperation(value="添加学员", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name",value = "名字"),
		@ApiImplicitParam(name = "sex",value = "性别"),
		@ApiImplicitParam(name = "phone",value = "电话"),
		@ApiImplicitParam(name = "date",value = "生日"),
		@ApiImplicitParam(name = "studentType",value = "标签")
	})
	public RespontDateJson<?> inser(
			@RequestParam("name")String name,
			@RequestParam("sex")String sex,
			@RequestParam("phone")String phone,
			@RequestParam("date")String date,
			@RequestParam("studentType")Integer studentType
			){		
		return noStudentImplUtil.inser(name, sex, date, phone, studentType);
	}
	
	
	
	
	@PostMapping("/delete")
	@ApiOperation(value="删除学员", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sid",value = "sid删除")
	})
	public RespontDateJson<?> delete(@RequestParam(value="sid",required = true)Integer sid){
		int index = -1;
		try {
			  index = noStudentMapper.deleteByPrimaryKey(sid);
		} catch (Exception e) {
			logger.error("delete from delete no-student-api :{}",sid);
		}
		return index>0?RespontDateJson.ok(index, "删除成功"):RespontDateJson.fuall(index, "删除失败");  
	}
	
	
	@PostMapping("/update")
	@ApiOperation(value="修改学员", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "sid",value = "sid",required = true),
		@ApiImplicitParam(name = "name",value = "名字",required = false),
		@ApiImplicitParam(name = "sex",value = "性别",required = false),
		@ApiImplicitParam(name = "phone",value = "电话",required = false),
		@ApiImplicitParam(name = "birthday",value = "生日",required = false),
		@ApiImplicitParam(name = "studentType",value = "标签",required = false)
	})
	public RespontDateJson<?> update(
			@RequestParam(value="sid",required = true)Integer sid,
			@RequestParam(value="name",required = false)String name,
			@RequestParam(value="sex",required = false)String sex,
			@RequestParam(value="phone",required = false)String phone,
			@RequestParam(value="birthday",required = false)String birthday,
			@RequestParam(value="studentType",required = false)Integer studentType
			){		
		try {
			return noStudentImplUtil.update(sid,name, sex, birthday, phone, studentType);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	
	@GetMapping("/byid")
	@ApiOperation(value="主键id查询", notes="")
	public RespontDateJson<?>SelectbyId(@RequestParam(value="sid",required = true)Integer sid){
		
		NoStudent student=noStudentMapper.selectByPrimaryKey(sid);
		return RespontDateJson.ok(student, "查询成功");
		
	}
	
	
}
